using RushMail.Requests;

namespace RushMail;

public interface IRushMailClient
{
    public PoolCategory PoolCategory { get; }
    public Pool Pool { get; }
    public PoolAddress PoolAddress { get; }
    public TemplateCategory TemplateCategory { get; }
    public Template Template{ get; }
    public Domain Domain{ get; }
    public DomainEmail DomainEmail{ get; }
    public Track Track{ get; }
    public Account Account{ get; }
    public Report Report{ get; }
    public RushMailTask Task{ get; }
    public Task<RushMailResponse> PostAsync(RushMailRequest request);
}

public class RushMailRequest
{
    public RushMailRequest(string module, string action)
    {
        Module = module;
        Action = action;
    }

    public string Module { get; }
    
    public string Action { get; }
    
    public Dictionary<string, string>? Body { get; set; }

    public string BuildUrl() => $"?module={Module}&action={Action}";
}

public class RushMailResponse
{
    public bool IsSuccessStatusCode { get; }
    public bool IsOk { get; }
    public int StatusCode { get; }
    public JObject Content { get; }
    
    public string? Message { get; }

    public JToken? Data => Content["data"];

    public RushMailResponse(bool isSuccessStatusCode, int statusCode, JObject content)
    {
        IsSuccessStatusCode = isSuccessStatusCode;
        StatusCode = statusCode;
        Content = content;
        IsOk = isSuccessStatusCode && content.Value<bool>("status");
        Message = content.Value<string>("message");
    }

    public RushMailResponse<TResult> ToGeneric<TResult>() => new(IsSuccessStatusCode, StatusCode, Content);
}

public class RushMailResponse<TResult> : RushMailResponse
{
    public RushMailResponse(bool isSuccessStatusCode, int statusCode, JObject content) : base(isSuccessStatusCode, statusCode, content)
    {
        Result = Data == null ? default : Data.ToObject<TResult>();
    }
    
    public TResult? Result { get; }
}